package com.medusa.aps.business.modules.modeling.controller;

import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.output.MaterialChooseVO;
import com.medusa.aps.business.modules.modeling.model.input.BomDTO;
import com.medusa.aps.business.modules.modeling.model.input.BomDetailParam;
import com.medusa.aps.business.modules.modeling.model.input.BomParam;
import com.medusa.aps.business.modules.modeling.model.input.ChooseMaterialRouteParam;
import com.medusa.aps.business.modules.modeling.model.output.BomVo;
import com.medusa.aps.business.modules.modeling.mp.entity.Bom;
import com.medusa.aps.business.modules.modeling.mp.service.BomService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Create;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * BOM项控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:42
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/bom")
@Tag(name = "BOM项控制层", description = "BOM项控制层")
public class BomController {
    @Resource
    private BomService bomService;

    /**
     * 分页查询BOM数据
     *
     * @param bomParam 查询实体
     * @return 分页数据
     */
    @GetMapping("/select/page/list")
    @Operation(summary = "分页查询BOM数据", description = "分页查询BOM数据", method = "GET")
    public Result<Page<Bom>> selectAll(@Parameter(description = "查询参数") BomParam bomParam) {
        return Result.ok(bomService.selectPageList(new Page<>(bomParam.getCurrent(), bomParam.getSize()), bomParam));
    }

    /**
     * 查看BOM明细
     *
     * @return 单条数据
     */
    @GetMapping("/select/bom/detail")
    @Operation(summary = "查看BOM明细", description = "查看BOM明细", method = "GET")
    public Result<BomVo> selectBomDetail(@Schema(description = "BOM明细参数") BomDetailParam bomDetailParam) {
        return Result.ok(bomService.selectBomDetail(bomDetailParam));
    }

    /**
     * BOM选择物料
     *
     * @param chooseMaterialRouteParam 查询参数
     */
    @GetMapping("/choose/material")
    @Operation(description = "BOM选择物料", summary = "BOM选择物料")
    public Result<Page<MaterialChooseVO>> chooseMaterial(ChooseMaterialRouteParam chooseMaterialRouteParam) {
        return Result.ok(bomService.chooseMaterial(chooseMaterialRouteParam));
    }

    /**
     * 新增Bom
     *
     * @param bomDTO 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    @Operation(summary = "新增Bom", description = "新增Bom", method = "POST")
    public Result<Boolean> save(@RequestBody @Validated(Create.class) @Parameter(description = "BOM数据传输对象") BomDTO bomDTO) {
        bomService.saveBom(bomDTO);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param bomDTO 实体对象
     * @return 修改结果
     */
    @PutMapping(value = "/update", produces = "application/json;charset=utf-8")
    @Operation(summary = "修改BOM", description = "修改BOM", method = "PUT")
    public Result<Boolean> update(@RequestBody @Validated(Update.class) BomDTO bomDTO) {
        bomService.updateBom(bomDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param bomIds 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除BOM", description = "删除BOM", method = "DELETE")
    public Result<Boolean> delete(@Parameter(description = "BOM Id集合") @NotNull(message = "请选择需要删除的BOM") @RequestBody @Size(min = 1, message = "至少删除一条数据") List<Integer> bomIds) {
        bomService.deleteBomByIds(bomIds);
        return Result.ok();
    }

}

